<?php
//用户兑换产品订单管理
/**
 * Created by PhpStorm.
 * User: win7
 * Date: 2018/7/4 0004
 * Time: 10:02
 */
namespace Wx\Controller;

use Wx\Model\ExchangeQrcodeModel;

class UserExchangeorderController extends CommonController{

	public function index(){
		$state=$_GET['state'];
		$where='';
		if($state!='')$where=' and state='.$state;
        $this->assign('state',$state);
		
		$exchange_goods=D('exchange_goods');
        $category = M('category');
		$exchange_order=D('exchange_order');
		$member=D('member');
		
		$order=$exchange_order->where("state!=4 and u_id=".session('u_id').$where)->select();
		foreach($order as $i=>$one){
			$getorder=$exchange_goods->where("id=".$one['g_id'])->find();
			
			$img=explode(',',$getorder['head_img']);
            $order[$i]['f_img']=$img[0];//第一张图片作为封面图
            $order[$i]['unit']=$category->where("id=".$getorder['unit'])->getField('title');

            $order[$i]['title']=$getorder['title'];
            $order[$i]['price']=$getorder['price'];
            $order[$i]['g_u_id']=$getorder['u_id'];

		}//end foreach
        $this->assign('order',$order);
		
		$this->display('order-dh');
	}//end fun

    //订单详情页
    public function detail(){
	    $order_id = $_GET['oid'];
	    if ($order_id){
	        $order = M("exchange_order")->where("id = {$order_id}")->find();//订单信息
            $u_c_id = M("member")->where("id = ".$order['u_id'])->getField('c_id');//用户小区id
            $store = M("shop_store")->where("c_id = {$u_c_id}")->field("c_id,realname,mobile,address,storename")->find();//门店信息
            $goods = M("exchange_goods")->where("id = {$order['g_id']}")->find();//商品信息
            $evaluate = M("exchange_evaluate")->where("o_id = {$order['id']}")->order('addtime desc')->select();//评论信息
            foreach ($evaluate as $k => $v){
                $evaluate[$k]['pic'] = explode(',',$v['pic']);//评论图片
                $evaluate[$k]['member'] = getMember($v['u_id']);//用户信息
                if($v['in_inco'] == 0){
                    $evaluate[$k]['member']['nickname'] = "匿名";
                }//end if
            }//end foreach

            if($order['state'] == 4){
                $this->error("该订单已删除!");return;
            }else{
                $this->assign('store',$store);
                $this->assign("order",$order);
                $this->assign("goods",$goods);
                $this->assign("evaluate",$evaluate);
                $this->display("orderdetail-dh");
            }//end if
//            dump($order);
        }else{
	        $this->error("访问错误");
        }
    }//end if

    //评价
    public function evaluate(){
        if(IS_POST){
            $post = I('post.');
            $post['u_id'] = session('u_id');    // 用户id
            if($post['img_1']!=''||$post['img_2']!=''||$post['img_3']!=''){//如果有图片便进行处理
                $post['pic'] = $post['img_1'].$post['img_2'].$post['img_3'];    //拼接图片路径
                $post['pic'] = substr($post['pic'],0,strlen($post['pic'])-1);   //去掉路径最后的英文逗号
            }// end if
//            $this->ajaxReturn($post,"JSON");return;
            $post['addtime'] = time();
            $res = M('exchange_evaluate')->add($post);
            if($res) {
                // 更新订单状态-已评价
                $eva = M('exchange_order')->where('id = '.$post['o_id'])->setField('state',3);
                if ($eva){
                    $this->ajaxReturn(['state'=>0,'info'=>'添加成功'],"JSON");
                }//end if
            }else{
                $this->ajaxReturn(['state'=>1,'info'=>'添加失败'],"JSON");
            }// end if
        }else{
            $o_id = intval(I('oid'));  //订单id
            $order = M("exchange_order")->where("id = {$o_id}")->getField('g_id');
            $g_id = intval($order);  // 商品id
            $this -> assign('o_id',$o_id);
            $this -> assign('g_id',$g_id);
            $this->display('evaluate');
        }// end if
    }//end fun
	
	public function cancel(){
        $exchange_order = M("exchange_order");
        $order_id = $_POST['order_id'];//订单id
        $goods_id = $_POST['goods_id'];//商品id
        if ($order_id){
            $delete = $exchange_order->where("id = {$order_id}")->save(['state'=> 4]);
            if (!$delete){
                $this->error("取消失败");
            }else{
                M("exchange_goods")->where("id = {$goods_id}")->setInc('num',1);//退还库存
            }//end if
        }//end if
	}//end fun
}//end class